package com.mms.utils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
	public static final String Format_Date = "yyyy-MM-dd";
	public static final String Format_Time = "HH:mm:ss";
	public static final String Format_DateTime = "yyyy-MM-dd HH:mm:ss";

	public static String getCurrentDate(long timestramp){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestramp*1000L));
	}
	
	public static String getCurrentDate() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	public static String getCurrentDate(String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(new Date());
	}

	public static String getCurrentTime() {
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}

	public static String getCurrentTime(String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(new Date());
	}

	public static String getCurrentDateTime() {
		String format = "yyyy-MM-dd HH:mm:ss";
		return getCurrentDateTime(format);
	}

	public static int getDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		return cal.get(7);
	}

	public static int getDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(7);
	}

	public static int getDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		return cal.get(5);
	}

	public static int getDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(5);
	}

	public static int getMaxDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMaximum(5);
	}

	public static String getFirstDayOfMonth(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		cal.set(5, 1);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	public static int getDayOfYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(6);
	}

	public static int getDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(6);
	}

	public static int getDayOfWeek(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(7);
	}
	/**
	 * 获取两个日期字符间的其它日期
	 * @param start 大日期
	 * @param end  小日期
	 * * @param islast  是否需要最后一个值
	 * @return
	 */
    public static List<String> getDates(String start,String end,String format,boolean islast){
    	int num =DateUtils.getnumfromDate_(start, end);
    	if (!islast) num = num-1;
    	Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		Date date2 = null;
		List<String> dates = new ArrayList<String>();
		dates.add(sdf.format(DateUtils.parse(end)));
		for(int i=0;i < num ; i++){
			if (date == null){
			date2 = DateUtils.parse(end);
			}else{
				date2 =date;
			}
			calendar.setTimeInMillis(date2.getTime());
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			date = calendar.getTime();
			dates.add(sdf.format(date));
		}
		
    	return dates;
    }
    /**
     * 返回本月所有日期集合
     * @param forma
     * @return
     */
    public static List<String> getListofMonth(String forma){
    	
    	SimpleDateFormat format = new SimpleDateFormat(forma);
		Calendar   cal_1=Calendar.getInstance();//获取当前日期 
	      // cal_1.set(Calendar.MONTH, 5);
	        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
	      String  firstDay = format.format(cal_1.getTime());
	        //获取前月的最后一天
	       cal_1.set(Calendar.DAY_OF_MONTH, cal_1.getActualMaximum(Calendar.DAY_OF_MONTH));  
	     String last = format.format(cal_1.getTime());
    	return getDates(last,firstDay,forma,true);
    }
    /**
     * 返回指定月的所有天
     * @param forma
     * @return
     */
public static List<String> getListFromMonth(String forma,Integer month){
    	
    	SimpleDateFormat format = new SimpleDateFormat(forma);
		Calendar   cal_1=Calendar.getInstance();//获取当前日期 
		if (month != null){
	    	 cal_1.set(Calendar.MONTH,month-1);
	     } 
	        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
	      String  firstDay = format.format(cal_1.getTime());
	        //获取前月的最后一天
	       cal_1.set(Calendar.DAY_OF_MONTH, cal_1.getActualMaximum(Calendar.DAY_OF_MONTH));  
	     String last = format.format(cal_1.getTime());
    	return getDates(last,firstDay,forma,true);
    }
    
    /**
     * 获取指定日期的后一天
     * @param start
     * @param format
     * @param n 1/-1 后一天前一天
     * @return
     */
    public static String getDatebefore(String start,String format,int n){
    	Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		Date date2 = null;
			date2 = DateUtils.parse(start);
			calendar.setTime(date2);
			calendar.add(Calendar.DAY_OF_YEAR, n);
			date = calendar.getTime();
    	return sdf.format(date);
    }
    /**
     * 返回一个指定日期的前n天或后n天。
     * @param end 指定日期
     * @param format 格式
     * @param n 多少天
     * @param flag -1 /1
     * @return
     */
    public static List<String> getDatepres(String end,String format,int n,int flag){
    	Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		Date date2 = null;
		List<String> dates = new ArrayList<String>();
		 for(int i=0 ; i < n;i++){
			date2 = DateUtils.parse(end);
			calendar.setTime(date2);
			calendar.add(Calendar.DAY_OF_YEAR, flag);
			date = calendar.getTime();
			end = sdf.format(date);
			dates.add(end);
		 }
    	return dates;
    }
    
	public static int getDayOfMonth(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(5);
	}

	public static int getDayOfYear(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(6);
	}

	public static String getCurrentDateTime(String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(new Date());
	}

	public static String toString(Date date) {
		if (date == null) {
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	public static String toDateTimeString(Date date) {
		if (date == null) {
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	public static String toString(Date date, String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(date);
	}

	public static String toTimeString(Date date) {
		if (date == null) {
			return "";
		}
		return new SimpleDateFormat("HH:mm:ss").format(date);
	}

	public static int compare(String date1, String date2) {
		return compare(date1, date2, "yyyy-MM-dd");
	}

	public static int compareTime(String time1, String time2) {
		return compareTime(time1, time2, "HH:mm:ss");
	}

	public static int compare(String date1, String date2, String format) {
		Date d1 = parse(date1, format);
		Date d2 = parse(date2, format);
		return d1.compareTo(d2);
	}
	
	public static int compare(Date date1, Date date2, String format) {
		return compare(toString(date1, format), toString(date2, format), format);
	}
	
	public static int compare(Date date1, Date date2) {
		return compare(toString(date1, Format_DateTime), toString(date2, Format_DateTime), Format_DateTime);
	}

	public static int compareTime(String time1, String time2, String format) {
		String[] arr1 = time1.split(":");
		String[] arr2 = time2.split(":");
		if (arr1.length < 2) {
			throw new RuntimeException("错误的时间值:" + time1);
		}
		if (arr2.length < 2) {
			throw new RuntimeException("错误的时间值:" + time2);
		}
		int h1 = Integer.parseInt(arr1[0]);
		int m1 = Integer.parseInt(arr1[1]);
		int h2 = Integer.parseInt(arr2[0]);
		int m2 = Integer.parseInt(arr2[1]);
		int s1 = 0;
		int s2 = 0;
		if (arr1.length == 3) {
			s1 = Integer.parseInt(arr1[2]);
		}
		if (arr2.length == 3) {
			s2 = Integer.parseInt(arr2[2]);
		}
		if ((h1 < 0) || (h1 > 23) || (m1 < 0) || (m1 > 59) || (s1 < 0)
				|| (s1 > 59)) {
			throw new RuntimeException("错误的时间值:" + time1);
		}
		if ((h2 < 0) || (h2 > 23) || (m2 < 0) || (m2 > 59) || (s2 < 0)
				|| (s2 > 59)) {
			throw new RuntimeException("错误的时间值:" + time2);
		}
		if (h1 != h2) {
			return h1 > h2 ? 1 : -1;
		}
		if (m1 == m2) {
			if (s1 == s2) {
				return 0;
			}
			return s1 > s2 ? 1 : -1;
		}

		return m1 > m2 ? 1 : -1;
	}

	public static boolean isTime(String time) {
		String[] arr = time.split(":");
		if (arr.length < 2)
			return false;
		try {
			int h = Integer.parseInt(arr[0]);
			int m = Integer.parseInt(arr[1]);
			int s = 0;
			if (arr.length == 3) {
				s = Integer.parseInt(arr[2]);
			}
			if ((h < 0) || (h > 23) || (m < 0) || (m > 59) || (s < 0)
					|| (s > 59))
				return false;
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static boolean isDate(String date) {
		String[] arr = date.split("-");
		if (arr.length < 3)
			return false;
		try {
			int y = Integer.parseInt(arr[0]);
			int m = Integer.parseInt(arr[1]);
			int d = Integer.parseInt(arr[2]);
			if ((y < 0) || (m > 12) || (m < 0) || (d < 0) || (d > 31))
				return false;
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static boolean isWeekend(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int t = cal.get(7);
		return (t == 7) || (t == 1);
	}

	public static boolean isWeekend(String str) {
		return isWeekend(parse(str));
	}

	public static Date parse(String str) {
		if (StringUtils.isEmpty(str))
			return null;
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(str);
		} catch (ParseException e) {
		}
		return null;
	}

	public static Date parse(String str, String format) {
		if (StringUtils.isEmpty(str))
			return null;
		try {
			SimpleDateFormat t = new SimpleDateFormat(format);
			return t.parse(str);
		} catch (ParseException e) {
		}
		return null;
	}

	public static Date parseDateTime(String str) {
		if (StringUtils.isEmpty(str)) {
			return null;
		}
		if (str.length() <= 10)
			return parse(str);
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date parseDateTime(String str, String format) {
		if (StringUtils.isEmpty(str))
			return null;
		try {
			SimpleDateFormat t = new SimpleDateFormat(format);
			return t.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date addMinute(Date date, int count) {
		return new Date(date.getTime() + 60000L * count);
	}

	public static Date addHour(Date date, int count) {
		return new Date(date.getTime() + 3600000L * count);
	}

	public static Date addDay(Date date, int count) {
		return new Date(date.getTime() + 86400000L * count);
	}

	public static Date addWeek(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(3, count);
		return c.getTime();
	}

	public static Date addMonth(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(2, count);
		return c.getTime();
	}

	public static Date addYear(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(1, count);
		return c.getTime();
	}

	public static String toDisplayDateTime(String date) {
		if (StringUtils.isEmpty(date))
			return null;
		try {
			if (isDate(date)) {
				return toDisplayDateTime(parse(date));
			}
			SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = t.parse(date);
			return toDisplayDateTime(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "不是标准格式时间!";
	}
  public static String commonDate(){
	  SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  return t.format(new Date());
  }
  public static String getYearMonthDay(String date){
	  String[] s = date.split("-");
	  if (s.length == 3){
		  return s[0]+"年"+s[1]+"月"+s[2]+"日";
	  }
	  return date;
  }
  public static String getMonthDay(String date){
	  String[] s = date.split("-");
	  if (s.length == 3){
		  return s[1]+"月"+s[2]+"日";
	  }
	  return date;
  }
	/**
	 * @param date
	 * @return
	 */
	public static String toDisplayDateTime(Date date) {
		long minite = (System.currentTimeMillis() - date.getTime()) / 60000L;
		if (minite < 60L) {
			return toString(date, "MM-dd") + " " + minite + "分钟前";
		}
		if (minite < 1440L) {
			return toString(date, "MM-dd") + " " + minite / 60L + "小时前";
		}
		return toString(date, "MM-dd") + " " + minite / 1440L + "天前";
	}
	/**
	 * 比较两个日期的天数
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int getnumfromDate(Date d1,Date d2){
		long t1 = d1.getTime();
		long t2 = d2.getTime();
		
		return (int) ((t1-t2)/(1000 * 60 * 60 * 24));
	}
	/**
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int getnumfromDate(Date d1,String d2){
		
		long t1 = parseDateTime(getCurrentDate(Format_Date)).getTime();
		long t2 = parseDateTime(d2).getTime();
		
		return (int) ((t1-t2)/(1000 * 60 * 60 * 24));
	}
	public static int getnumfromDate_(String d1,String d2){

			long t1 = parseDateTime(d1).getTime();
			long t2 = parseDateTime(d2).getTime();

			return (int) ((t1-t2)/(1000 * 60 * 60 * 24));
		}

	public static int getMonthofDATE(String d1){


		return parseDateTime(d1).getMonth()+1;
	}
	/**
	 * 日期秒数返回具体格式
	 * @param d1
	 * @param format
	 * @return
	 */
	public static String secondTodate(String d1,String format){
		Long l = Long.parseLong(d1)*1000;
		SimpleDateFormat sm = new SimpleDateFormat(format);
		return sm.format(new Date(l));
	}
	/**
	 * 获取两个日期中有多少个工作日
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getDutyDays(String startDate,Date endDate) {
		int result = 0;
		Date sdate = parseDateTime(startDate);
		while (sdate.compareTo(endDate) <= 0) {
		if (sdate.getDay() != 6 && sdate.getDay() != 0)
		result++;
		sdate.setDate(sdate.getDate() + 1);
		}

		return result;
	}
	public static void main(String[] args) throws ParseException {
		System.out.println(getnumfromDate_("2015-10-16","2015-10-15"));
	}
	public static String format(long ms) {//将毫秒数换算成x天x时x分x秒x毫秒
		   int ss = 1000;
		   int mi = ss * 60;
		   int hh = mi * 60;
		   int dd = hh * 24;
		   long day = ms / dd;
		   long hour = (ms - day * dd) / hh;
		   long minute = (ms - day * dd - hour * hh) / mi;
		   long second = (ms - day * dd - hour * hh - minute * mi) / ss;
		   long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
		   String strDay = day < 10 ? "0" + day : "" + day;
		   String strHour = hour < 10 ? "0" + hour : "" + hour;
		   String strMinute = minute < 10 ? "0" + minute : "" + minute;
		   String strSecond = second < 10 ? "0" + second : "" + second;
		   String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
		   strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
		   return strDay + " " + strHour + ":" + strMinute + ":" + strSecond + " " + strMilliSecond;
		}
}
